package com.cifer.utils;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class TimeUtils {
    //比较两时间字符串的差值，以秒为单位,后面的时间减前面的时间
    public static Double timeInterVal(String time1,String time2){

        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
        LocalTime localTime1 = LocalTime.parse(time1.trim().substring(11) , pattern);
        LocalTime localTime2 = LocalTime.parse(time2.trim().substring(11) , pattern);
        long intervalN = ChronoUnit.NANOS.between(localTime1, localTime2);
        return intervalN/1000000000.0;


    }

    //将12-24-25-123格式的时间字符串转换成含有有效部分12:24:25的LocalTime对象
    public static LocalTime parse(String timeStr){
        String reTimeStr = timeStr.substring(0, 8).replace('-', ':');
        return LocalTime.parse(reTimeStr);
    }

//    public static void main(String[] args) {
//        String t1=" 2024-03-14 15:17:11.246";
//        String t2=" 2024-03-14 15:17:11.245";
//        System.out.println(timeInterVal(t2, t1));
//    }

}
